#include "library.h"

// 主菜单
void mainMenu() {
    int choice;
    while (1) {
        
        printf("====图书管理系统====\n");
        printf("(1)登录\n");
        printf("(2)注册\n");
        printf("(3)管理员登录\n");
        printf("(4)退出\n");
        
        int menu_1;
        scanf("%d", &menu_1);
        clearInputBuffer();

        switch (menu_1) {
            case 1:
                Login();
                break;
            case 2:
                Regist();
                break;
            case 3:
                AdminLogin();
                break;
            case 4:
                sqlite3_close(db);
                exit(0);
            default:
                printf("无效输出！\n");
                break;
    }
  }
}

// 管理员菜单
void AdminMenu(const char* username) {
 while (1) {
        printf("\n==== 管理员菜单 ====\n");
        printf("1. 用户信息管理\n");
        printf("2. 图书管理\n");
        printf("3. 借阅记录\n");
        printf("4. 修改密码\n");
        printf("5. 黑名单管理\n");
        printf("6. 退出登录\n");
        printf("请选择: ");
        
        int choice;
        scanf("%d", &choice);
        clearInputBuffer();
        switch (choice) {
            case 1:
                //StudentManagementMenu();
                UserManagementMenu();
                break;
            case 2:
                ManageBooks();
                break;
            case 3:
                ViewRecords();
                break;
            case 4:
                ChangePassword(username, 0);
                break;
            case 5:
                 BlacklistManagementMenu() ;   
                break;
            case 6:
                return;
            default:
                printf("无效选择，请重新输入！\n");
                break;
        }
    }

}

// 用户菜单
void UserMenu(const char* username) {
     while(1)
    {
        printf("\n======用户菜单======\n");
        printf("(1)信息查询\n");
        printf("(2)书籍借阅\n");
        printf("(3)书籍归还\n");
        printf("(4)书籍评价\n");
        printf("(5)修改密码\n");
        printf("(6)退出登录\n");
        int menu_2=0;   
        scanf("%d",&menu_2);
        clearInputBuffer();
        switch (menu_2)
        {
        case 1:
            //信息查询
            SelectMenu(username);
            break;
        case 2:
            //书籍借阅
            BookBorrow(username);
            break;
        case 3:
            //书籍归还
            BookReturn(username);
            break;
         case 4:
             //书籍评价
              addReview(username);
             break;
        case 5:
             ChangePassword(username,0);
            //修改密码
            break;
        case 6:
            //退出登录
            return ;
            break;
        default:
            printf("请正确选择！\n");
            break;
        }
    }

 
}




void SelectMenu(const char * username)
{
    while(1)
    {
        printf("\n======选择查找任务======\n");
        printf("(1)个人信息\n");  
        printf("(2)借阅历史\n");
        printf("(3)书籍评价\n");//去掉
        printf("(4)返回\n");
        int menu=0;
        scanf("%d",&menu);
        clearInputBuffer();

        switch (menu)
        {
        case 1:
            //个人信息
            PersonalInfo(username);
            break;
        case 2:
            //借阅历史
            RecordHistory(username);
            break;
        case 3:
            viewUserReviews(username);
            //书籍评价
            //去掉
            break;
        case 4:
            return;
            break;
        default:
            break;
        }
    }
}




void UserManagementMenu() {
    while (1) {
        printf("\n==== 用户信息管理 ====\n");
        printf("1. 查看所有用户信息\n");
        printf("2. 查看用户详细信息\n");
        printf("3. 修改用户信息\n");
        printf("4. 重置用户密码\n");
        printf("5. 删除用户信息\n");
        printf("6. 返回上级菜单\n");
        printf("请选择: ");
        
        int choice;
        scanf("%d", &choice);
        clearInputBuffer();

        switch (choice) {
            case 1:
                ViewAllUsers();
                break;
            case 2: {
                char username[32];
                memset(username, 0, sizeof(username));
                printf("请输入用户名: ");
                scanf("%s", username);
                clearInputBuffer();
                ViewUserDetails(username);
                break;
            }
            case 3:
                ModifyUserInfo();
                break;
            case 4:
                ResetUserPassword();
                break;
            case 5:
                DeleteUser();
                break;
            case 6:
                return;
            default:
                printf("无效选择，请重新输入！\n");
                break;
        }
    }
}



void ManageBooks() {
    while (1) {
        printf("\n==== 图书信息管理 ====\n");
        printf("1. 增加图书信息\n");
        printf("2. 修改图书信息\n");
        printf("3. 删除图书信息\n");
        printf("4. 查询图书信息\n");
        printf("5. 查看所有图书\n");
        printf("6. 返回上级菜单\n");
        printf("请选择操作: ");
        
        int choice;
        scanf("%d", &choice);
        clearInputBuffer();

        switch (choice) {
            case 1:
                AddBook();
                break;
            case 2:
                ModifyBookInfo();
                break;
            case 3:
                DeleteBookInfo();
                break;
            case 4:
                SearchBookInfo();
                break;
            case 5:
                listAllBooks();
                break;
            case 6:
                return;
            default:
                printf("无效选择，请重新输入1-5之间的数字！\n");
                // 清空输入缓冲区
                while (getchar() != '\n');
                break;
        }
    }
}














